<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

//define root system dir
define("API_ROOT_PATH", dirname(__FILE__));
define("CONFIG_PATH", API_ROOT_PATH . '/config');
define("SYSTEM_PATH", API_ROOT_PATH . "/system");
define("LOG_PATH", API_ROOT_PATH . '/logs');


// import all class needed
require CONFIG_PATH . '/version.cnf.php';

// import code define
require CONFIG_PATH . '/code.cnf.php';

//import database config
require CONFIG_PATH . '/db.cnf.php';


//check version is suported
$ver = isset($_GET['v']) ? $_GET['v'] : "1";
if ($ver >= MIN_VERSION_SUPPORT && $ver <= MAX_VERSION_SUPPORT) {

//import system class
    $system_class_path = SYSTEM_PATH . "/v$ver/system.class.php";
    require_once $system_class_path;
//import all customize class
   $list_class = find_all_files(SYSTEM_PATH . "/v$ver/class");
   if(count($list_class) > 0){
      foreach($list_class as $values){
         require_once $values;
      }
   }
   
//run application
    $application = new Application(API_ROOT_PATH . "/v$ver");
    $application->run();
} else {
    echo '{"code":' . CODE_BAD_REQUEST . ',"message":"Version is not suport!"}';
}
?>